package com.kilomob.grady.codec;

import io.netty.channel.ChannelPipeline;

import java.util.Map;

import com.google.common.collect.ClassToInstanceMap;
import com.google.common.collect.MutableClassToInstanceMap;
import com.kilomob.grady.handler.NettyReceiveHandler;
import com.kilomob.grady.handler.netty.KryoReceiveHandler;
import com.kilomob.grady.handler.netty.ProtostuffReceiveHandler;

public class ReceiveSerializeFrame implements SerializeFrame {

    private Map<String, Object> handlerMap = null;

    public ReceiveSerializeFrame(Map<String, Object> handlerMap) {
        this.handlerMap = handlerMap;
    }

    private static ClassToInstanceMap<NettyReceiveHandler> handler = MutableClassToInstanceMap.create();

    static {
        handler.putInstance(ProtostuffReceiveHandler.class, new ProtostuffReceiveHandler());
        handler.putInstance(KryoReceiveHandler.class, new KryoReceiveHandler());
    }

    public void select(String protocol,String id, ChannelPipeline pipeline) {
        switch (protocol) {
            case "pt": {
                handler.getInstance(ProtostuffReceiveHandler.class).handle(handlerMap, pipeline);
                break;
            }
            case "ky": {
                handler.getInstance(KryoReceiveHandler.class).handle(handlerMap, pipeline);
                break;
            }
            default:
                handler.getInstance(ProtostuffReceiveHandler.class).handle(handlerMap, pipeline);
                break;
        }
    }
}
